/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.market.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.modules.market.entity.EventLottery;
import com.kaaction.modules.market.dao.EventLotteryDao;

/**
 * 抽奖算法Service
 * @author zengbiao
 * @version 2017-04-19
 */
@Service
@Transactional(readOnly = true)
public class EventLotteryService extends CrudService<EventLotteryDao, EventLottery> {

	public EventLottery get(String id) {
		return super.get(id);
	}
	
	public List<EventLottery> findList(EventLottery eventLottery) {
		return super.findList(eventLottery);
	}
	
	public Page<EventLottery> findPage(Page<EventLottery> page, EventLottery eventLottery) {
		return super.findPage(page, eventLottery);
	}
	
	@Transactional(readOnly = false)
	public void save(EventLottery eventLottery) {
		super.save(eventLottery);
	}
	
	@Transactional(readOnly = false)
	public void delete(EventLottery eventLottery) {
		super.delete(eventLottery);
	}
	
	public List<EventLottery> findByEventId(String eventId) {
		return super.dao.findByEventId(eventId);
	}
	
	//抽奖算法
	public int draw(List<EventLottery> list){
		//首先根据概率，计算中奖可能
		if(list != null && list.size() > 0) {
			int total = list.size();
			if(total == 4) {
				EventLottery firstPrize = list.get(0);
				EventLottery secPrize = list.get(1);
				EventLottery thirdPrize = list.get(2);
				EventLottery fourPrize = list.get(3);
				if(firstPrize != null && secPrize != null && thirdPrize != null && fourPrize != null) {
					double firstPrizeWeight = firstPrize.getWeight();
					double secPrizeWeight = secPrize.getWeight();
					double thirdPrizeWeight = thirdPrize.getWeight();
					double fourthPrizeWeight = fourPrize.getWeight();
					
					int firstPrizeRemain = Integer.parseInt(firstPrize.getQuantityLeft());
					int secPrizeRemain = Integer.parseInt(secPrize.getQuantityLeft());
					int thirdPrizeRemain = Integer.parseInt(thirdPrize.getQuantityLeft());
					int fourPrizeRemain = Integer.parseInt(fourPrize.getQuantityLeft());
					
					double allPrizeWeight = firstPrizeWeight + secPrizeWeight + thirdPrizeWeight + fourthPrizeWeight;
					double randomNum = Math.random() * 100;
					System.out.println("randomNum = " + randomNum);
					if(randomNum < allPrizeWeight){
						//中奖了
						if(randomNum < firstPrizeWeight && firstPrizeRemain > 0){
							return 1;//一等奖，在网页中的编号（0到8）
						} else if(randomNum < firstPrizeWeight + secPrizeWeight && secPrizeRemain > 0){
							//即使中了1等奖，如果1等奖被领完了，只能折腾到2等奖
							return 2;
						}else if(randomNum < firstPrizeWeight + secPrizeWeight + thirdPrizeWeight && thirdPrizeRemain > 0){
							//即使中了1,2等奖，如果1,2等奖被领完了，只能折腾到3等奖
							return 3;
						}else if(fourPrizeRemain > 0){
							//即使中了1,2,3等奖，如果1,2,3等奖被领完了，只能折腾到4等奖
							return 4;
						}  
					} 
				} 
			}
		}
		return 0;//规则不合理：一律认定为没中奖
	}
	
}